<template>
    <div class="block">
        <div style="border:1px lightgray dotted;padding-bottom:5px;">
            <el-row>
                <exInput label="保存路径" v-model="savePath" width="550" size="mini"></exInput>
                <el-button type="primary" @click="selFolder" size="mini" style="height:28px;margin-top:4px">选择目录</el-button>
            </el-row>
            <el-row>
                
                <exInput label="url" v-model="url" width="550" size="mini"></exInput>
                <el-button type="primary" @click="openWebSite" size="mini" style="height:28px;margin-top:4px">开始下载网站</el-button>
                <el-button type="primary" @click="getDlInfo" size="mini" style="height:28px;margin-top:4px">获取处理列表</el-button>
                <!-- <el-button type="primary" @click="dealFilesPath" size="mini" style="height:28px;margin-top:4px">链接处理</el-button> -->
            </el-row>
        </div>
        <exPubTable ref="curTable" :cfg.sync="newcfg" :exData="dataList" class="mt10">  
        </exPubTable>
    </div>
</template>



<script>
import Vue from "vue";

import cheerio from "cheerio"

export default {
    data() {
        var that = this;
        return {
            isLoad: false,
            savePath : "C:/Users/flyswatter/Desktop/test/动漫设计公司工作室通用响应式网站模板下载",
            url : "http://dfj-20210524-1.demo.mxyhn.xyz/index.html",
            
            dataList:[],
            newcfg:{
                usePage:false,
                showIndex: true,
                columns: [
                    {prop:"url",label:"url",width:300},
                    {prop:"filepath",label:"文件路径",width:300},
                    {prop:"filename",label:"文件名",width:200},
                    {prop:"filetype",label:"文件类型",width:120,sortable:true},
                    {prop:"subpath",label:"保存路径",width:100},
                    {prop:"statusCode",label:"http状态",width:100},
                    {prop:"progress",label:"下载进度",endStr:"%",width:100},
                    {prop:"isEnd",label:"是否结束",width:100}

                ],
            },
        }
    },
    computed:{
    },
    provide () {
      return {
      }
    },

    mounted() {
        this.getDlInfo()
    },

    methods: {
        openWebSite(){
            this.initDlInfo();
            // window.open(this.url);
        },
        initDlInfo(){
            var that = this;
            this.registerListen(this,"initDlInfoRes",(err)=>{
                // try{
                //     if(err){
                //         that.$message.error("配置保存异常:"+err)
                //     }else{
                //         that.$message.success("保存成功")
                //     }
                // }catch(ex){
                //         that.$message.error("配置保存异常:"+err)
                // }
            });
            localStorage.setItem("websiteDL_savePath",this.savePath)
            localStorage.setItem("websiteDL_url",this.url)
            electron.ipcRenderer.send("initDlInfo",this.savePath,this.url)
        },
        getDlInfo(){
            var that = this;
            this.registerListen(this,"getDlInfo",(res)=>{
                try{
                    if(res.isSucc){
                        this.dataList = res.info.files;
                        this.savePath = res.info.savePath;

                        var tmpsavePath = localStorage.getItem("websiteDL_savePath");
                        var tmpurl = localStorage.getItem("websiteDL_url");
                        this.savePath = tmpsavePath || this.savePath;
                        this.url = tmpurl || this.url;
                    }else{
                        that.$message.error(res.info)
                    }
                }catch(ex){
                    that.$message.error("获取下载信息异常:"+err)
                }
            });
            
            electron.ipcRenderer.send("getDlInfo")
        },
        dealFilesPath(){
            var that = this;
            this.registerListen(this,"dealFilesPath",(res)=>{
                try{
                    if(res.isSucc){
                    }else{
                        that.$message.error(res.info)
                    }
                }catch(ex){
                    that.$message.error("链接处理异常:"+err)
                }
            });
            
            electron.ipcRenderer.send("dealFilesPath")
        },
        selFolder(){
            var that = this;
            this.registerListen(this,"selFile",(res)=>{
                try{
                    if(res){
                        res = res.replace(/\\/g,"/");
                        this.savePath = res;
                    }
                }catch(ex){
                    that.$message.error("获取目录失败:"+err)
                }
            });
            
            electron.ipcRenderer.send("selFile",'openDirectory')
            // electron.ipcRenderer.send("selFile",'openFile')
        },
        query(){
            if(!this.url) return;
            this.pageLink = [];
            this.dataList = [];
            this.cnt = 0;
            this.$axios.get(this.url+this.lang)
            .then((res) => {
                try{
                    var html = res.data;
                    this.readListInfo(html,true);
                }catch(ex){

                }
            })
        },
    }
    
}
</script>
